#!/bin/bash

set -e

info_msg="\e[0;32m[INFO]\e[0;30m"
error_msg="\e[0;31mFAILED\e[0;30m"

function output_error_log {
  [[ -f error.log ]] && ( cat error.log >&2; rm error.log)
}

echo -ne "$info_msg Checking if ruby installed? "
which 'ruby' >/dev/null 2>error.log || ( echo -e "$error_msg\n\nCould not find \`ruby\`. Please install ruby or add it to PATH"; output_error_log; exit 1 )
echo OK

echo -en "$info_msg rubygem \"rake\" "
gem install rake >/dev/null 2>error.log || ( echo -e "$error_msg\n\nAn error occurred during installation of rake. Run \`gem install rake\` yourself."; output_error_log; exit 1 )
echo OK

if [ -n "$CI" ]; then
  echo -e "$info_msg Bootstrapping in CI mode"
  rake_task='bootstrap:ci'
else
  echo -e "$info_msg Bootstrapping in normal mode"
  rake_task='bootstrap'
fi

echo -en "$info_msg Running rake task \"$rake_task\" "
rake --trace $rake_task >error.log 2>&1 || ( echo -e "$error_msg\n\nAn error occurred during run of rake-task \"$rake_task\". Run \`rake $rake_task\` yourself."; output_error_log; exit 1 )
echo OK
